<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload Files using XMLHttpRequest - Minimal</title>
<script type="text/javascript">
      function fileSelected() { //文件选择更改时调用的事件
        var file = document.getElementById('fileToUpload').files[0]; //获得文件上传信息
        if (file) { //如果用户选择了文件（没有选择的话，file就是null或是undefined，这样可以判断）
          var fileSize = 0; //文件大小
          if (file.size > 1024 * 1024) //如果文件大小大于1MB
            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; //转换文件大小并以MB单位显示
          else //否则
            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; //否则用KB单位显示
 
          document.getElementById('fileName').innerHTML = ' ' + file.name; //显示文件名信息
          document.getElementById('fileSize').innerHTML = ' ' + fileSize; //显示文件大小信息
          document.getElementById('fileType').innerHTML = ' ' + file.type; //显示文件MIME信息
        }
      }
 
      function uploadFile() { //点击上传按钮时的时间
        var fd = new FormData(); //FormData是Html5的新增类
        fd.append("file", document.getElementById('fileToUpload').files[0]); //
        var file = document.getElementById('fileToUpload').files[0];
        var xhr = new XMLHttpRequest();
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);
        fd.append("filename",file.name);
        fd.append("filesize",file.size);
        fd.append("filetype",file.type);
        xhr.open("POST", "Html5FileUpload");
        xhr.send(fd);
      }
 
      function uploadProgress(evt) {
        if (evt.lengthComputable) {
          var percentComplete = Math.round(evt.loaded * 100 / evt.total);
          document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
        }
        else {
          document.getElementById('progressNumber').innerHTML = 'unable to compute';
        }
      }
 
      function uploadComplete(evt) {
        /* This event is raised when the server send back a response */
        alert(evt.target.responseText);
        document.getElementById('progressNumber').innerHTML="100%";
      }
 
      function uploadFailed(evt) {
        alert("There was an error attempting to upload the file.");
      }
 
      function uploadCanceled(evt) {
        alert("The upload has been canceled by the user or the browser dropped the connection.");
      }
</script>
</head>
<body>
<form id="form1" enctype="multipart/form-data" method="post" action="upload.php">
<div class="row">
<label for="fileToUpload">文件选择：</label>
<input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/>
</div>
文件信息：<br/>
<table id="file_info">
	<tr>
		<td>文件名：</td>
		<td><span id="fileName">未选择文件</span></td>
	</tr>
	<tr>
		<td>文件大小：</td>
		<td><span id="fileSize">未选择文件</span></td>
	</tr>
	<tr>
		<td>文件MIME类型：</td>
		<td><span id="fileType">未选择文件</span></td>
	</tr>
	<tr>
		<td>文件上传进度：</td>
		<td><span id="progressNumber">0%</span></td>
	</tr>
</table>
<div class="row">
<input type="button" onclick="uploadFile()" value="Upload" />
</div>
</form>
</body>
</html>